<?php  if( !defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('get_report_csv')) 
{
	function get_report_csv($cols, $rows, $pre, $title = NULL) 
	{
		$CI =& get_instance();
		
		$config = array(
			'type' => 'table',
			'cols' => $cols,
			'tr' => $rows,
			'dir' => FCPATH . 'temp/',
			'prefix' => $pre,
			'title' => $title,
		);

		_delete_old_csv();
		$CI->load->library('CsvCreator', $config);
		if ($CI->csvcreator->create_csv()) return $CI->csvcreator->get_filename();
	}
}

if ( ! function_exists('_delete_old_csv'))
{
	/**
	 * Delete 3 days old csv files
	 *
	 * @access public
	 * @return void
	 */ 
	function _delete_old_csv()
	{
		foreach (glob('./temp/*.csv') as $csv)
		{
			((time() - fileatime($csv)) >= 259200) && @unlink($csv);
		}
	}
}
?>
